package com.mantru.iotpot.setting.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.DateClass.Calibration_date;
import com.mantru.iotpot.R;

import java.util.List;

public class PipelineSelfCleaningAdapter extends RecyclerView.Adapter<PipelineSelfCleaningAdapter.ViewHolder> {
    private List<Calibration_date> calibrationDates;
    private ApplicationClass ac;
    private Context context;
    @NonNull
    @Override
    public PipelineSelfCleaningAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_pipeline_self_cleaning, parent, false);
        return new PipelineSelfCleaningAdapter.ViewHolder(view);
    }

    public PipelineSelfCleaningAdapter(List<Calibration_date> calibrationDates, ApplicationClass ac,Context context) {
        this.calibrationDates = calibrationDates;
        this.ac = ac;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(@NonNull PipelineSelfCleaningAdapter.ViewHolder holder, int position) {
        Calibration_date calibrationDate = calibrationDates.get(position);
        holder.name.setText(calibrationDate.getNozzle_name());
        // 为 Switch 添加 OnCheckedChangeListener
        holder.mswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int nozzleId = Integer.parseInt(calibrationDate.getNozzle_id());
                if (isChecked) {
                    ac.Calibration_Control_Nozzle(nozzleId, 1);
                    ac.SaveLog("清洗的管道id： " + nozzleId + " 操作： " + 1);
                } else {
                    ac.Calibration_Control_Nozzle(nozzleId, 0);
                    ac.SaveLog("清洗的管道id： " + nozzleId + " 操作： " + 0);
                }
            }
        });

        // 设置初始状态
        holder.mswitch.setChecked(false);
    }

    @Override
    public int getItemCount() {
        return calibrationDates.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView name;
        private final Switch mswitch;


        public ViewHolder(View view){
            super(view);
            name = view.findViewById(R.id.adapter_pipeline_self_cleaning_name);
            mswitch = view.findViewById(R.id.adapter_pipeline_self_cleaning_switchOilHeat);
        }
    }
}
